package net.yeah.mickey_zhouyou.database.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapUtil {

	public static <T1, T2> Entry<T1, T2> unmodifiableEntry(Entry<T1, T2> entry) {
		return makeUnmodifiableEntry(entry.getKey(), entry.getValue());
	}

	public static <T1, T2> Entry<T1, T2> makeUnmodifiableEntry(final T1 obj1,
			final T2 obj2) {
		return new Entry<T1, T2>() {

			public T1 getKey() {
				return obj1;
			}

			public T2 getValue() {
				return obj2;
			}

			public T2 setValue(T2 value) {
				throw new UnsupportedOperationException();
			}

		};
	}

	public static <T1, T2> Set<Entry<T1, T2>> unmodifiableEntrySet(
			final Set<Entry<T1, T2>> set) {
		return new Set<Entry<T1, T2>>() {
			public int size() {
				return set.size();
			}

			public boolean isEmpty() {
				return set.isEmpty();
			}

			public boolean contains(Object o) {
				return set.contains(o);
			}

			public Iterator<Entry<T1, T2>> iterator() {
				class InnerIterator implements Iterator<Entry<T1, T2>> {
					InnerIterator(Iterator<Entry<T1, T2>> it) {
						this.it = it;
					}

					private Iterator<Entry<T1, T2>> it;

					public boolean hasNext() {
						return it.hasNext();
					}

					public Entry<T1, T2> next() {
						return unmodifiableEntry(it.next());
					}

					public void remove() {
						throw new UnsupportedOperationException();
					}
				}
				return new InnerIterator(set.iterator());
			}

			public Object[] toArray() {
				return set.toArray();
			}

			public <T> T[] toArray(T[] a) {
				return set.toArray(a);
			}

			public boolean containsAll(Collection<?> c) {
				return set.containsAll(c);
			}

			public boolean retainAll(Collection<?> c) {
				throw new UnsupportedOperationException();
			}

			public boolean add(Entry<T1, T2> e) {
				throw new UnsupportedOperationException();
			}

			public boolean addAll(Collection<? extends Entry<T1, T2>> c) {
				throw new UnsupportedOperationException();
			}

			public boolean remove(Object o) {
				throw new UnsupportedOperationException();
			}

			public boolean removeAll(Collection<?> c) {
				throw new UnsupportedOperationException();
			}

			public void clear() {
				throw new UnsupportedOperationException();
			}
		};
	}

	public static <T1, T2> Map<T1, T2> unmodifiableMap(final Map<T1, T2> map) {
		return new Map<T1, T2>() {
			public int size() {
				return map.size();
			}

			public boolean isEmpty() {
				return map.isEmpty();
			}

			public boolean containsKey(Object key) {
				return map.containsKey(key);
			}

			public boolean containsValue(Object value) {
				return map.containsValue(value);
			}

			public T2 get(Object key) {
				return map.get(key);
			}

			public Set<T1> keySet() {
				return map.keySet();
			}

			public Collection<T2> values() {
				return map.values();
			}

			public Set<java.util.Map.Entry<T1, T2>> entrySet() {
				return unmodifiableEntrySet(map.entrySet());
			}

			public T2 put(T1 key, T2 value) {
				throw new UnsupportedOperationException();
			}

			public T2 remove(Object key) {
				throw new UnsupportedOperationException();
			}

			public void putAll(Map<? extends T1, ? extends T2> m) {
				throw new UnsupportedOperationException();
			}

			public void clear() {
				throw new UnsupportedOperationException();
			}
		};
	}

}
